<?php

/**
 * @author Samrat Khan & Sadik Sarfaraz - 11:06:03 AM Aug 22, 2013 
 */
//require 'foo.config.php';
ob_start();
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'final_warehouse';
//$table = 'stockin';
$file = 'Faulty_export';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

//$result = mysql_query("SHOW COLUMNS FROM " . $table . "");
//if (mysql_num_rows($result) > 0) {
//    while ($row = mysql_fetch_assoc($result)) {
//        $csv_output .= $row['Field'] . ", ";
//        $i++;
//    }
//}
//$csv_output .= "\n";

//$values = selectAllMatchColumn('stockin', 'stockout', 'serialno', 'serialno', $limit, 'cus_id', $sess_cusID, $db);
$values = mysql_query("
       SELECT t1.serialno,
            (SELECT projectname FROM projects WHERE t1.project_id = projects.project_id) AS ProjectName,
            t1.productid,
            t1.description,
            t1.revisionstate,
            t1.boxno,
            (SELECT measurement FROM uom WHERE t1.uom = uom.uom_id) AS UOM,
            t1.faulty_export_quantity,
            t1.handedoverto,
            t1.manufacturingdate,
            t1.faultydocketnumber,
            t1.docketdate,
            t1.siteid,
            (SELECT warehousename FROM warehouses WHERE t1.warehouse_id = warehouses.warehouse_id) AS WarehouseName,
            t1.rr_rma,
            t1.export_rma,
            t1.rr_invoiceno,
            t1.rr_date,
            t1.rr_mawb,
            t1.rr_awb,
            t1.remarks, 
            (SELECT username FROM wms_users WHERE t1.userid = wms_users.id) AS UserName,
            (SELECT cusname FROM customerinfo WHERE t1.cus_id = customerinfo.cus_id) AS CustomerName,
            t1.inserteddate
        FROM faulty_export AS t1 WHERE junkentry = '1'
        ");
$headings = array( 'Serial No', 'Project Name','Product ID','Description', 'Revision State','Box No','UOM','Faulty Export Quantity',
                    'Handed Over To','Manufacturing Date','Faulty Docket Number','Docket Date',  
                     'Site Name','Warehouse Name','Raised RMA','Export RMA', 'Return Export Invoice No', 'Return Export Date','Return Export MAWB',
                      'Return Export AWB','Remarks','Username','Customer Name','Inserted Date');
$i = 0;
foreach ($headings as $heading) {
    $csv_output .= $heading . ", ";
    $i++;
}
$csv_output .= "\n";
while ($rowr = mysql_fetch_row($values)) {
for ($j = 0; $j < $i; $j++) {
        $csv_output .= '"'.$rowr[$j] . '",';
    }
$csv_output .= "\n";
}
ob_end_clean();



$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
//header("Content-type: text/csv");
header('Content-Length: ' . strlen($csv_output) );
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>